package com.saifan.wyy_ov.ui.property;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.data.bean.RepairBean;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.common.BaseFragment;

import com.saifan.wyy_ov.ui.view.RefreshLayout;
import com.saifan.wyy_ov.utils.CommonAdapter;
import com.saifan.wyy_ov.utils.DensityUtil;
import com.saifan.wyy_ov.utils.StringUtil;
import com.saifan.wyy_ov.utils.ToastUtil;
import com.saifan.wyy_ov.utils.ViewHolderHelper;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class RepairProgressFragment extends BaseFragment implements AdapterView.OnItemClickListener {

    private int page = 0;

    boolean isLoad;

    int type = 1;
    private CommonAdapter<RepairBean> adapter;
    private List<RepairBean> beans = new ArrayList<>();
    private ListView listView;
    private RefreshLayout refreshLayout;
    private View view;
    private View loading_lay;

    public RepairProgressFragment() {

    }


    public void query() {
        GetData getData = new GetData();
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("FKGX_ZJ", getRoomInfoBean().getFKGX_ZJ());
            jsonObject.put("Page", page);
            jsonObject.put("Type", type);
            if (beans.isEmpty()) {
                loading_lay.setVisibility(View.VISIBLE);
            }
            getData.getDataJson(getActivity(), Api.Matter.MATTER_INFO, jsonObject, "", new ResponseData() {
                @Override
                public void onSuccess(String json) {

                    List<RepairBean> list = new Gson().fromJson(json, new TypeToken<List<RepairBean>>() {
                    }.getType());

                    if (!list.isEmpty()) {
                        if (page == 0) {
                            beans = list;
                        } else {
                            beans.addAll(list);
                        }

                        adapter.mDatas = beans;
                        adapter.notifyDataSetChanged();
                        isLoad = true;

                    } else {
                        if (page > 0) {
                            page--;
                        } else {

                        }

                    }

                    loading_lay.setVisibility(View.GONE);
                    refreshLayout.setRefreshing(false);
                    refreshLayout.setLoading(false);


                }

                @Override
                public void onFailed(String str) {

                    if (page > 0) page--;
                    loading_lay.setVisibility(View.GONE);
                    refreshLayout.setRefreshing(false);
                    refreshLayout.setLoading(false);
                }
            });

        } catch (JSONException e) {


        }


    }

    @Override
    public void onResume() {
        super.onResume();
        if (!isLoad) {
            query();
        }


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_repair_progress, container, false);
        loading_lay = view.findViewById(R.id.loading_lay);
        this.refreshLayout = (RefreshLayout) view.findViewById(R.id.refreshLayout);
        this.listView = (ListView) view.findViewById(R.id.listView);
        adapter = new CommonAdapter<RepairBean>(getActivity(), beans, R.layout.repair_recoder_item) {
            @Override
            public void convert(ViewHolderHelper helper, RepairBean item) {
                helper.setText(R.id.SJ, StringUtil.returnShow(item.getWYBSQD_FSSJ()));
                helper.setText(R.id.BT, StringUtil.returnShow(item.getWYBSQD_BSLX()));
                helper.setText(R.id.LR, StringUtil.returnShow(item.getWYBSQD_BSLR()));
                helper.setText(R.id.BM, "处理部门：" + StringUtil.returnShow(item.getWYBSQD_GWMC()));
                com.flyco.labelview.LabelView label = helper.getView(R.id.labelView);
                if (item.isWYBSQD_SFJD() && !item.isWYBSQD_SFHF() && !item.isWYBSQD_SFWC()) {

                    label.setBgColor(getResources().getColor(R.color.lable2));
                    label.setText("处理中");


                } else if (item.isWYBSQD_SFWC() && !item.isWYBSQD_SFHF()) {
                    label.setBgColor(getResources().getColor(R.color.lable3));
                    label.setText("已完成");
                } else if (item.isWYBSQD_SFHF()) {
                    label.setBgColor(getResources().getColor(R.color.lable1));
                    label.setText("已回访");
                } else {
                    label.setBgColor(getResources().getColor(R.color.state3));
                    label.setText("已受理");
                }


            }
        };
        listView.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // TODO Auto-generated method stub
                page = 0;
                refreshLayout.setRefreshing(true);
                query();
            }
        });

        refreshLayout.setOnLoadListener(new RefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {
                // TODO Auto-generated method stub

                refreshLayout.setLoading(true);
                page++;
                query();
            }
        });

        listView.setOnItemClickListener(this);
        return view;
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity(), ProgressItemActivity.class);
        intent.putExtra("data", beans.get(position));
        startActivity(intent);


    }
}